<?php


namespace app\admin\controller;


use app\common\model\Money;
use app\common\model\WechatShareLink;
use app\user\controller\Vip;
use app\user\model\TtUser;
use think\Controller;
use think\Db;
use think\facade\Request;
use app\common\model\Setting;
use app\common\model\User as UserModel;

class User extends  Controller
{

    protected $middleware = [
        'app\http\middleware\checkAdminToken::class'
    ];

    //直接开通会员
    public function openVip($vip_user_id)
    {

        $user=\app\user\model\User::get( $vip_user_id);
        $user->vip=1;
        $user->save();

        $setting= Setting::getAllSetting();

        //给一级分销加钱，增加可提现的余额
        if($user->lv_1_id!=0 && $setting['lv_1_share_income_money']){
            $lv_1_id=$user->lv_1_id;
            $lv_1_info=\app\user\model\User::get( $lv_1_id);

            //直推是会员的话，给直推分佣
            if($lv_1_info->vip==1 ||$lv_1_info->vip==2){
                $res2=Money::addCanTakeOutMoney($user->lv_1_id,$setting['lv_1_share_income_money']);
            }
        }

        // 给二级分销加钱，增加可提现的余额
        if($user->lv_2_id!=0 && $setting['lv_2_share_income_money']){
            $lv_2_id=$user->lv_2_id;
            $lv_2_info=\app\user\model\User::get( $lv_2_id);

            //间推是会员的话，给间推分佣
            //如果用户的间推是高级会员或者普通会员拥有两级分销的权限，就加钱
            if($setting['lv_1_share_type']==2 || $lv_2_info->vip==2){
                $res2=Money::addCanTakeOutMoney($user->lv_2_id,$setting['lv_2_share_income_money']);
            }
        }

        $this->result($user,200,'开通成功');
    }

    //清空会员
    public function closeVip($vip_user_id)
    {
        $user=\app\user\model\User::get( $vip_user_id);
        $old_vip=$user->vip;        //关闭会员之前，先存下来
        $user->vip=0;
        $user->save();
        $this->result($user,200,'清空成功');
    }

    //修改用户的钱包
    public function editUserMoney( )
    {
        $user_id=Request::param('user_id');
        $can_take_out=Request::param('can_take_out');
        $user=\app\user\model\User::get( $user_id);
        $user->can_take_out=$can_take_out;
        $user->save();
        $this->result($user,200,'余额已经修改为：'.$can_take_out);
    }

    //开通高级会员
    public function setUserHighVip($vip_user_id)
    {

        $user=\app\user\model\User::get( $vip_user_id);
        $user->vip=2;
        $user->save();

        $setting= Setting::getAllSetting();

        //给一级分销加钱，增加可提现的余额
        if($user->lv_1_id!=0  ){
            //用户上级的资料
            $lv_1_id=$user->lv_1_id;
            $lv_1_info=\app\user\model\User::get( $lv_1_id);

            //
            if($lv_1_info->vip==1){
                $res2=Money::addCanTakeOutMoney($user->lv_1_id,$setting['lv_1_share_income_money']);
            }

            if($lv_1_info->vip==2){
                $res2=Money::addCanTakeOutMoney($user->lv_1_id,$setting['high_vip_lv_1_share_income_money']);
            }
        }

        // 给二级分销加钱，增加可提现的余额
        if($user->lv_2_id!=0 ){
            //用户上上级的资料
            $lv_2_id=$user->lv_2_id;
            $lv_2_info=\app\user\model\User::get( $lv_2_id);

            //推荐人是高级会员
            if($lv_2_info->vip==2){
                $res2=Money::addCanTakeOutMoney($user->lv_2_id,$setting['high_vip_lv_2_share_income_money']);
            }

            //推荐人是普通会员
            if($lv_2_info->vip==1){
                //如果普通会员可以两级分销，就直接给普通会员的佣金
                if($setting['lv_1_share_type']==2){
                    $res2=Money::addCanTakeOutMoney($user->lv_2_id,$setting['high_vip_lv_1_share_income_money']);
                }
            }
        }

        $this->result($user,200,'开通成功');
    }

    //加入黑名单
    public function joinBlackList()
    {
        $user_id=Request::param('user_id');
        $res=\app\user\model\User::where([
            ['id','=', $user_id],        //拉黑人员
        ])->update([
            'status'=>0
        ]);
        $result=WechatShareLink::where([
            ['user_id','=', $user_id],        //删除视频
        ])->update([
            'user_id'=>0
        ]);
        if($res){
            $this->result($res,200,'已经成功将用户加入黑名单');
        }else{
            $this->result($res,200,'该用户已经拉黑');
        }
    }

    //给会员封号和解封都是走这个
    public function  changeUserStatus(){
        $user_id=Request::param('user_id',0);
        $user_info=\app\user\model\User::get($user_id);

        if($user_info->status==1){
            $user_info->status=0;
          $res=  $user_info->save();
            $this->result($res,200,'已成功拉黑');
        }else{
            $user_info->status=1;
            $res=  $user_info->save();
            $this->result($res,200,'已成功解封');
        }

    }

    //生成分销激活码
    public function createCode()
    {
        $vip_user_id=Request::param('vip_user_id');
        $user=\app\user\model\User::get($vip_user_id);

        if($user->vip_code!=0){
            $this->result(null,400,'请勿重复生成');
        }else{
            $code = str_pad(mt_rand(0, 999999), 6, "0", STR_PAD_BOTH);
            $user->vip_code=$code;
            $user->save();
            $this->result(null,200,'成功');
        }
    }

    //抖音用户列表
    public function dyUserList()
    {
        $page  = Request::param('page', 1) ;
        $pageSize = Request::param('pageSize', 20);           //哪一天的数据

        $key = Request::param('key', '');
        if (is_numeric($key)) {
            $where2 = 'user.id=' . $key;
        } else {
            $where2 = "user.username like  '%$key%' ";
        }

        $list = \app\user\model\User:: where($where2)
            ->withAttr('add_time', function($value, $data) {
                return date("Y-m-d H:i:s", $value);
            })
            ->page($page, $pageSize)->select();

        $count =  \app\user\model\User:: where($where2)->count('id');

        $data = [
            'list' => $list,
            'page_num' => ceil($count / $pageSize),
            'page' => $page
        ];

        $this->result($data, 200);
    }

    //用户列表
    public function index()
    {

        $page  = Request::param('page', 1) ;
        $pageSize = Request::param('pageSize', 20);           //哪一天的数据

        $key = Request::param('key', '');
        if (is_numeric($key)) {
            $where2 = 'user.id=' . $key;
        } else {
            $where2 = "user.username like  '%$key%' ";
        }

        $list = \app\user\model\User:: where($where2)
            ->withAttr('add_time', function($value, $data) {
                return date("Y-m-d H:i:s", $value);
            })
            ->page($page, $pageSize)->order('id desc')->select();

        $count =  \app\user\model\User:: where($where2)->count('id');

        $data = [
            'list' => $list,
            'page_num' => ceil($count / $pageSize),
            'page' => $page
        ];

        $this->result($data, 200);
    }

    //分销列表
    public function shareUserList()
    {

        $share_user_id = Request::param('share_user_id');
        $lv = Request::param('lv', '1');       //一级分销还是二级分销
        $page = Request::param('page', 1);       //一级分销还是二级分销
        $where = [
            'user.lv_' . $lv . '_id' => $share_user_id
        ];

        $list =\app\common\model\User::where($where)
            ->leftJoin('user_income', 'user.id=user_income.user_id')
            ->field('user.id , user.username,user.mobile, user.avatar, user.vip, user.lv_1_id,user.lv_2_id,sum(user_income.lv_1_income) as sum_lv_1_income ,sum(user_income.lv_2_income) as sum_lv_2_income')
            ->group('user.id')
            ->page($page, 20)
            ->select();

//        $list = UserModel::where($where)
//            ->leftJoin('user_income', 'user.id=user_income.user_id')
//            ->field('user.id , user.username, user.avatar,  sum(user_income.lv_1_income) as sum_lv_1_income ,sum(user_income.lv_2_income) as sum_lv_2_income')
//            ->group('user_income.user_id')
//            ->page($page, 20)
//            ->select();

        $count = UserModel::where("lv_".$lv."_id=$share_user_id")->field('id')->count('id');

        $data=[
            'list'=>$list,
            'page'=>$page,
            'page_num'=> ceil($count/200)
        ];
        $this->result($data, 200, '');
    }


}
